package com.zy.find;

/**
 * __|__ __|__
 * _|_   _|_
 * .---. .---.
 * |___| |___|
 * _\_/_ _\_/_
 * _X_   _X_
 * |   | |   |
 * |___| |___|
 *
 * @Author zhaoyi
 * @date 2020/4/10
 * 求一个数的开根号 精确到小数点后6位
 */
public class Radical {

    public static void main(String[] args) {
        double value = 8d;
        double v = compareValue(0d, value, value);
        System.out.println(v);

    }

    private static double compareValue(double left, double right, double value) {

        double mid = (left + right) / 2;
        if (String.valueOf(mid).split("\\.")[1].length() >= 6) {
            return mid;
        }

        if (mid * mid == value) {
            return mid;
        } else if (mid * mid > value) {
            return compareValue(left, mid, value);
        } else {
            return compareValue(mid, right, value);
        }
    }
}
